<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing: template
 *
 * @category   	Joomzf Cms
 * @package    	Template
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 May 27, 2012 - 4:54:47 PM
 */
class Joomzf_Template{
	
	private $_path;
	private $_url;
	
	private $_configFile 		= 'template.ini';
	private $_configFileSection = 'default';
	
	public $action;
	public $area;

	/**
	 * @param Joomzf_Controller_Action $action
	 * @param array $options
	 */
	public function __construct(Joomzf_Controller_Action $action,array $options = null){
		$this->action = $action;
		$this->area   = $action->getArea();
		$this->loadPath();
		$this->load();
		
	}
	
	public function load(){
		
		$action = $this->action;
		
		$action->view->headScript()->getContainer()->exchangeArray(array());
		$action->view->headMeta()->getContainer()->exchangeArray(array());
		$action->view->headLink()->getContainer()->exchangeArray(array());
		
		$config  = new Zend_Config_Ini($this->_path . DS . $this->_configFile,$this->_configFileSection);
		$arrConfig = $config->toArray();
		
		$action->view->headTitle($arrConfig['title'],true);
		
		$metaHttp = $arrConfig['metaHttp'];
		if(count($metaHttp)>0){
			foreach ($metaHttp as $val){
				$tmp = explode('||', $val);
				$action->view->headMeta()->appendHttpEquiv($tmp[0],$tmp[1]);
			}
		}
		
		$metaName = $arrConfig['metaName'];
		if(count($metaName)>0){
			foreach ($metaName as $val){
				$tmp = explode('||', $val);
				$action->view->headMeta()->appendName($tmp[0],$tmp[1]);
			}
		}
		$cssUrl = $this->_url.$arrConfig['dirCss'];
		$fileCss = $arrConfig['fileCss'];
		if(count($fileCss)>0){
			foreach ($fileCss as $val){				
				$action->view->headLink()->appendStylesheet($cssUrl . $val,'screen' );
			}
		}
		
		$jsUrl = $this->_url. $arrConfig['dirJs'];
		$fileJs = $arrConfig['fileJs'];
		if(count($fileJs)>0){
			foreach ($fileJs as $val){				
				$action->view->headScript()->appendFile($jsUrl . $val);
			}
		}
		
		if(count($arrConfig['dirs'])>0){
			foreach ($arrConfig['dirs'] as $val){
				$arrDir[$val] = $this->_url.DS. $val;
			}
		}
		
		$options = array(
						'layout'=>$arrConfig['layout'],
						'layoutPath'=>$this->_path,						
						'viewSuffix'=>'phtml',						
						);
		// tranfer for using on module
		$action->view->request  = $action->getRequest();
		
		$action->view->template  = new stdClass();
		$action->view->template->area = $this->area;
		$action->view->template->path = $this->_path;
		$action->view->template->url = $this->_url;
		
		Zend_Layout::startMvc($options);
		$instance = Zend_Layout::getMvcInstance();
	}
	
	public function loadPath(){
		$db = Zend_Registry::get("DBO");
		$sql = $db->select()->from(array("tpl"=>TBL_PRF."template_styles"),array("tpl.template"))
							->where("home = 1")
							->where("area = ?",$this->area);
		$template = $db->fetchOne($sql);
		$this->_path = ($this->area ==0) ? FRONTEND_TEMPLATE_PATH.DS.$template : BACKEND_TEMPLATE_PATH.DS.$template; 
		$this->_url = ($this->area ==0) ? FRONTEND_TEMPLATE_URL.DS.$template : BACKEND_TEMPLATE_URL.DS.$template; 
		
	}
	
	public function setPath(String $value = null){
		$this->_path = $value;
	}
	
	public function getPath(){
		return $this->_path;
	}
	
	public function setconfigFile(String $value){
		if($value == null) $value = "template.ini";
		$this->_configFile = $value;
	}
	
	public function getconfigFile(){
		return $this->_configFile;
	}
	
	public function setconfigFileSection(String $value){
		if($value == null) $value = "default";
		$this->_configFileSection = $value;
	}
	
	public function getconfigFileSection(){
		return $this->_configFileSection;
	}
	
}